<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.studentmanagement.model.User" %>
<%@ page import="com.studentmanagement.model.Enrollment" %>
<%@ page import="com.studentmanagement.util.PageBean" %>
<%@ page import="java.util.List" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%
    // 检查用户是否已登录
    User user = (User) session.getAttribute("user");
    if (user == null) {
        response.sendRedirect(request.getContextPath() + "/login");
        return;
    }
    
    // 获取分页数据
    PageBean<Enrollment> pageBean = (PageBean<Enrollment>) request.getAttribute("pageBean");
    if (pageBean == null) {
        response.sendRedirect(request.getContextPath() + "/enrollment");
        return;
    }
    
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>选课记录列表 - 学生管理系统</title>
    <link rel="stylesheet" href="<%= request.getContextPath() %>/css/style.css">
</head>
<body>
    <div class="dashboard">
        <header class="header">
            <h1>学生管理系统</h1>
            <div class="user-info">
                欢迎, <%= user.getUsername() %> | <a href="<%= request.getContextPath() %>/logout">退出</a>
            </div>
        </header>
        
        <nav class="sidebar">
            <ul>
                <li><a href="<%= request.getContextPath() %>/student">学生管理</a></li>
                <li><a href="<%= request.getContextPath() %>/course">课程管理</a></li>
                <li><a href="<%= request.getContextPath() %>/enrollment" class="active">选课管理</a></li>
                <li><a href="<%= request.getContextPath() %>/dashboard.jsp">返回控制面板</a></li>
            </ul>
        </nav>
        
        <main class="content">
            <h2>选课记录列表</h2>
            
            <div class="actions">
                <a href="<%= request.getContextPath() %>/enrollment/add" class="btn">添加选课记录</a>
            </div>
            
            <div class="pagination-controls">
                <form action="<%= request.getContextPath() %>/enrollment" method="post">
                    <div class="form-group">
                        <label for="pageSize">每页显示:</label>
                        <select id="pageSize" name="pageSize" onchange="this.form.submit()">
                            <option value="5" <%= pageBean.getPageSize() == 5 ? "selected" : "" %>>5</option>
                            <option value="10" <%= pageBean.getPageSize() == 10 ? "selected" : "" %>>10</option>
                            <option value="20" <%= pageBean.getPageSize() == 20 ? "selected" : "" %>>20</option>
                            <option value="50" <%= pageBean.getPageSize() == 50 ? "selected" : "" %>>50</option>
                        </select>
                    </div>
                    
                    <div class="form-group">
                        <label for="sortField">排序字段:</label>
                        <select id="sortField" name="sortField">
                            <option value="enrollment_id" <%= "enrollment_id".equals(pageBean.getSortField()) ? "selected" : "" %>>选课ID</option>
                            <option value="enrollment_date" <%= "enrollment_date".equals(pageBean.getSortField()) ? "selected" : "" %>>选课日期</option>
                            <option value="grade" <%= "grade".equals(pageBean.getSortField()) ? "selected" : "" %>>成绩</option>
                        </select>
                    </div>
                    
                    <div class="form-group">
                        <label for="sortOrder">排序方式:</label>
                        <select id="sortOrder" name="sortOrder">
                            <option value="ASC" <%= "ASC".equals(pageBean.getSortOrder()) ? "selected" : "" %>>升序</option>
                            <option value="DESC" <%= "DESC".equals(pageBean.getSortOrder()) ? "selected" : "" %>>降序</option>
                        </select>
                    </div>
                    
                    <button type="submit" class="btn">应用</button>
                </form>
            </div>
            
            <table class="data-table">
                <thead>
                    <tr>
                        <th>选课ID</th>
                        <th>学生</th>
                        <th>课程</th>
                        <th>选课日期</th>
                        <th>成绩</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <% 
                    List<Enrollment> enrollments = pageBean.getData();
                    if (enrollments != null && !enrollments.isEmpty()) {
                        for (Enrollment enrollment : enrollments) {
                    %>
                    <tr>
                        <td><%= enrollment.getEnrollmentId() %></td>
                        <td><%= enrollment.getStudentName() %></td>
                        <td><%= enrollment.getCourseName() %></td>
                        <td><%= dateFormat.format(enrollment.getEnrollmentDate()) %></td>
                        <td><%= enrollment.getGrade() %></td>
                        <td>
                            <a href="<%= request.getContextPath() %>/enrollment/edit?id=<%= enrollment.getEnrollmentId() %>" class="btn-small">编辑</a>
                            <a href="<%= request.getContextPath() %>/enrollment/delete?id=<%= enrollment.getEnrollmentId() %>" class="btn-small btn-danger" onclick="return confirm('确定要删除该选课记录吗？')">删除</a>
                        </td>
                    </tr>
                    <% 
                        }
                    } else {
                    %>
                    <tr>
                        <td colspan="6">没有找到选课记录</td>
                    </tr>
                    <% } %>
                </tbody>
            </table>
            
            <div class="pagination">
                <% if (pageBean.isHasPreviousPage()) { %>
                    <a href="<%= request.getContextPath() %>/enrollment?pageNumber=<%= pageBean.getPageNumber() - 1 %>&pageSize=<%= pageBean.getPageSize() %>&sortField=<%= pageBean.getSortField() %>&sortOrder=<%= pageBean.getSortOrder() %>" class="btn-small">上一页</a>
                <% } %>
                
                <span class="page-info">
                    第 <%= pageBean.getPageNumber() %> 页，共 <%= pageBean.getTotalPages() %> 页，总记录数 <%= pageBean.getTotalRecords() %>
                </span>
                
                <% if (pageBean.isHasNextPage()) { %>
                    <a href="<%= request.getContextPath() %>/enrollment?pageNumber=<%= pageBean.getPageNumber() + 1 %>&pageSize=<%= pageBean.getPageSize() %>&sortField=<%= pageBean.getSortField() %>&sortOrder=<%= pageBean.getSortOrder() %>" class="btn-small">下一页</a>
                <% } %>
            </div>
        </main>
        
        <footer class="footer">
            <p>&copy; 2023 学生管理系统. All rights reserved.</p>
        </footer>
    </div>
</body>
</html>

